package com.yaozheng.vocationaltraining.service.impl;

import java.util.HashMap;

import net.sf.json.JSONObject;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EBean;

import com.yaozheng.vocationaltraining.iview.BaseView;
import com.yaozheng.vocationaltraining.iview.IBaseView;
import com.yaozheng.vocationaltraining.iview.IHomeAnswerTaskView;
import com.yaozheng.vocationaltraining.service.HomeAnswerTaskService;
import com.yaozheng.vocationaltraining.utils.Constants;
import com.yaozheng.vocationaltraining.utils.ErrorUtils;
import com.yaozheng.vocationaltraining.utils.http.okhttp.OkHttpClientManager;

@EBean
public class HomeAnswerTaskServiceImpl implements HomeAnswerTaskService {
    IHomeAnswerTaskView iHomeAnswerTaskView;

    @Override
    public void init(IHomeAnswerTaskView iHomeAnswerTaskView) {
        this.iHomeAnswerTaskView = iHomeAnswerTaskView;
    }

    @Background
    @Override
    public void getQuestionList() {
        try {
            OkHttpClientManager.getInstance().get(
                    Constants.ROOTURL + "/exam/daily",
                    iHomeAnswerTaskView.getToken(),
                    new OkHttpClientManager.IBaseViewCallback(new BaseView(iHomeAnswerTaskView) {

                        @Override
                        public void responseSuccess(JSONObject jsonObject) {
                            iHomeAnswerTaskView
                                    .getQuestionListSuccess(jsonObject);
                        }

                        @Override
                        public void responseError(String error) {
                            iHomeAnswerTaskView.getQuestionListError(error);
                        }
                    }));
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
            if (iHomeAnswerTaskView.isResponseAnswerResult()) {
                iHomeAnswerTaskView
                        .getQuestionListError(ErrorUtils.SERVER_CONNECTION_ERROR);
            }

        }
    }

    @Background
    @Override
    public void submitAnswer(String id, String answers) {
        try {
            HashMap<String, String> para = new HashMap<String, String>();
            para.put("id", id);
            para.put("answers", answers);
            OkHttpClientManager.getInstance().post(
                    Constants.ROOTURL + "/exam/daily",
                    iHomeAnswerTaskView.getToken(), para,
                    new OkHttpClientManager.IBaseViewCallback(new BaseView(iHomeAnswerTaskView) {

                        @Override
                        public void responseSuccess(JSONObject jsonObject) {
                            iHomeAnswerTaskView
                                    .submitAnswerSuccesss(jsonObject);
                        }

                        @Override
                        public void responseError(String error) {
                            iHomeAnswerTaskView.submitAnswerError(error);
                        }

                        @Override
                        public boolean isResponseResult() {
                            return iHomeAnswerTaskView.isResponseAnswerResult();
                        }
                    }));
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
            if (iHomeAnswerTaskView.isResponseAnswerResult()) {
                iHomeAnswerTaskView
                        .submitAnswerError(ErrorUtils.SERVER_CONNECTION_ERROR);
            }

        }
    }

}
